package per.sunmes.cl.g.sys;

import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.Pixmap;
import com.badlogic.gdx.graphics.Pixmap.Format;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.Texture.TextureFilter;
import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator;
import com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator.FreeTypeFontParameter;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.Group;
import com.badlogic.gdx.scenes.scene2d.Touchable;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.utils.Align;

public class U {

	public static Texture texture(String name) {
		if (G.asset.isLoaded(name, Texture.class)) {
			return G.asset.get(name);
		} else {
			G.asset.load(name, Texture.class);
			G.asset.finishLoadingAsset(name);
			return G.asset.get(name);
		}
	}

	public static void disTouch(Actor a) {
		a.setTouchable(Touchable.disabled);
	}

	public static TextureRegion region(String name) {
		Texture t = texture(name);
		if (t.getMagFilter() != TextureFilter.Linear
				|| t.getMinFilter() != TextureFilter.Linear) {
			t.setFilter(TextureFilter.Linear, TextureFilter.Linear);
		}
		return new TextureRegion(t);
	}

	public static Image image(String name) {
		return new Image(region(name));
	}

	public static FileHandle getFile(String name) {
		return G.fileResolver.resolve(name);
	}

	public static Label msg(String msg) {
		return G.gameActor.showMsg(msg);
	}

	public static Label msg(String msg, float delta) {
		return G.gameActor.showMsg(msg, delta);
	}

	// //////////////////
	private static final String FONT_GENERATOR = "freefont";
	private static String ttfFont = "font/DroidSansFallback.ttf";

	public static FreeTypeFontGenerator getFontGenerator() {
		FreeTypeFontGenerator generator = (FreeTypeFontGenerator) G.caches
				.get(FONT_GENERATOR);
		if (generator == null) {
			generator = new FreeTypeFontGenerator(U.getFile(ttfFont));
			G.caches.put(FONT_GENERATOR, generator);
		}
		return generator;
	}

	private static final String CARD_NAME_FONT = "cardName";
	private static final String CARD_TXT_FONT = "cardTxt";

	public static BitmapFont getCardNameFont() {
		BitmapFont font = (BitmapFont) G.caches.get(CARD_NAME_FONT);
		if (font == null) {
			FreeTypeFontGenerator g = getFontGenerator();
			FreeTypeFontParameter param = new FreeTypeFontParameter();
			param.incremental = true;
			param.color = Color.WHITE;
			param.color = Color.WHITE;
			param.magFilter = TextureFilter.Linear;
			param.minFilter = TextureFilter.Linear;
			param.size = 30;
			param.borderColor = Color.BLACK;
			param.borderWidth = 2f;
			font = g.generateFont(param);
			G.caches.put(CARD_NAME_FONT, font);
		}
		return font;
	}

	public static BitmapFont getCardTxtFont() {
		BitmapFont font = (BitmapFont) G.caches.get(CARD_TXT_FONT);
		if (font == null) {
			FreeTypeFontGenerator g = getFontGenerator();
			FreeTypeFontParameter param = new FreeTypeFontParameter();
			param.incremental = true;
			param.color = Color.WHITE;
			param.magFilter = TextureFilter.Linear;
			param.minFilter = TextureFilter.Linear;
			param.size = 24;
			font = g.generateFont(param);
			G.caches.put(CARD_TXT_FONT, font);
		}
		return font;
	}

	// //////////
	private static final String DEFAULT_FONT_CACHE_NAME = "gsDefaultFont";

	public static BitmapFont defaultFont() {
		BitmapFont font = (BitmapFont) G.caches.get(DEFAULT_FONT_CACHE_NAME);
		if (font == null) {
			font = new BitmapFont();
			G.caches.put(DEFAULT_FONT_CACHE_NAME, font);
		}
		return font;
	}

	private static final String MASK_TEXTURE_CACHE_NAME = "maskTexture";

	public static Image createMask() {
		Texture tex = (Texture) G.caches.get(MASK_TEXTURE_CACHE_NAME);
		if (tex == null) {
			Pixmap p = new Pixmap(8, 8, Format.RGBA8888);
			p.setColor(Color.WHITE);
			p.fill();
			tex = new Texture(p);
			p.dispose();
			G.caches.put(MASK_TEXTURE_CACHE_NAME, tex);
		}
		Image img = new Image(tex);
		stageSize(img);
		return img;
	}

	public static void stageSize(Actor actor) {
		actor.setSize(G.stage.getWidth(), G.stage.getHeight());
	}

	public static void centerParent(Actor a, Actor g) {
		a.setPosition(g.getWidth() / 2, g.getHeight() / 2, Align.center);
	}

	public static void center(Actor a, Actor to) {
		a.setPosition(to.getX() + to.getWidth() / 2, to.getY() + to.getHeight()
				/ 2, Align.center);
	}

	public static boolean rectHit(float px, float py, float x, float y,
			float width, float height) {
		if (px < x || px > x + width || py < y || py > y + height) {
			return false;
		}
		return true;
	}

	public static boolean actorHit(Actor a, float x, float y) {
		if (x < a.getX() || x > a.getX() + a.getWidth() || y < a.getY()
				|| y > a.getHeight() + a.getY()) {
			return false;
		}
		return true;
	}

	private static final Color drtColor = new Color();

	public static void drawRegionByActor(Batch batch, TextureRegion region,
			Actor actor, float parentAlpha) {
		drtColor.set(batch.getColor());
		batch.setColor(actor.getColor());
		batch.getColor().a = actor.getColor().a * parentAlpha;

		batch.draw(region, actor.getX(), actor.getY(), actor.getOriginX(),
				actor.getOriginY(), actor.getWidth(), actor.getHeight(),
				actor.getScaleX(), actor.getScaleY(), actor.getRotation());

		batch.setColor(drtColor);
	}

	private static final Vector2 pos = new Vector2();

	public static void actorKeepRectChangeParent(Actor a, Group g) {
		float cx, cy, wx, wy;
		pos.set(a.getWidth(), a.getHeight());
		a.localToStageCoordinates(pos);
		wx = pos.x;
		wy = pos.y;
		pos.set(a.getWidth() / 2, a.getHeight() / 2);
		a.localToStageCoordinates(pos);
		cx = pos.x;
		cy = pos.y;
		// //
		pos.set(0, 0);
		a.localToStageCoordinates(pos);
		g.stageToLocalCoordinates(pos);
		g.addActor(a);
		a.setPosition(pos.x, pos.y);
		pos.set(wx, wy);
		g.stageToLocalCoordinates(pos);
		wx = pos.x;
		wy = pos.y;
		pos.set(cx, cy);
		g.stageToLocalCoordinates(pos);
		cx = pos.x;
		cy = pos.y;
		a.setScale((wx - a.getX()) / a.getWidth(),
				(wy - a.getY()) / a.getHeight());

		a.setPosition(cx, cy, Align.center);
	}

}
